<template>
  <div class="login_container">
    <div class="login_box">
      <div class="login_form">
        <el-form ref="Ref" :model="form" :rules="rules">
          <div class="cn">
            轻量级培养方案管理系统
          </div>
          <el-form-item prop="username">
            <el-input v-model="form.username" type="text" placeholder="用户名" clearable prefix-icon="el-icon-user"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="form.password" type="password" placeholder="密码" clearable prefix-icon="el-icon-lock"></el-input>
          </el-form-item>

          <el-form-item class="btn_sub">
            <el-button type="primary" plain @click="login"  style="width: 100px">登录</el-button>
          </el-form-item>

        </el-form>
      </div>
    </div>
  </div>

</template>

<script>
  export const routes=[
    {
      path:'/Teacher_home',name:"teacher"
    }
  ]
  export default {
    name: 'login',
    variable:'',
    data () {
      return {
        form: {
          username: '',
          password: ''
        },
        rules: {
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ],
        }
      }
    },
    created() {
      this.variable=this.$route.query.variable
      window.sessionStorage.setItem('token',"Pub_home")
    },
    methods: {
      async login () {
        this.$refs.Ref.validate(async valid =>{
          if (!valid) return;
          const { data: res } = await this.$http.get("/login?username=" + this.form.username + "&password=" + this.form.password);
          if(res ==="manager"){
            this.$message.success('欢迎您，管理员' + this.form.username);
            window.sessionStorage.setItem('token',"manager_home")
            window.sessionStorage.setItem('role',"Manager")
            this.$router.push({ path: "/Manager_home"});
          }
          else if(res ==="teacher"){
            this.$session.set("bull",this.form.username)
            this.$message.success('欢迎您，课程负责人' + this.form.username);
            window.sessionStorage.setItem('token',"teacher_home")
            window.sessionStorage.setItem('role',"Teacher")
            this.$router.push({ path: "/Teacher_home"}).catch(() =>{});

          }
          else if(res ==="professor"){
            this.$session.set("bull",this.form.username)
            this.$message.success('欢迎您，专业负责人' + this.form.username);
            window.sessionStorage.setItem('token',"professor_home")
            window.sessionStorage.setItem('role',"Leader")
            this.$router.push({path:"/Leader_home"});
          }else {
            this.$message.error('登录失败');
          }

        })
      },
      register () {
        console.log('register!')
      }
    }
  }
</script>

<style scoped>
  .login_container{
    background: url("../assets/bac.jpg");
    width: 100%;
    height: 100%;
    background-size: cover;
  }
  .login_box{
    background-color: #ffffff;
    width: 500px;
    height: 290px;
    left: 50%;
    margin-left: -230px;
    border-radius: 20px;
    box-shadow: 0 0 30px rgba(0,0,0,.1);
    padding: 60px 0 30px 0;
    position: fixed;
    top: 15%;
  }

  .login_form{
    position: absolute;
    top: 80px;
    left: 13%;
    width: 366px;
  }
  .login_form .btn_sub{
    position: absolute;
    right: 0;
  }
  .login_form .forget{
    font-size: 10px;
    position: absolute;
    bottom: -75px;
    right: 0px;
    text-decoration: none;
    color: #acacad;
  }
  .login_form .normal{
    font-size: 10px;
    position: absolute;
    bottom: -34px;
    left: 0px;
    text-decoration: none;
    color: #777777;
  }

  .cn{
    font-size: 25px;
    margin-bottom: 40px;
    text-align: center;
    color: transparent;
    background-color : black;
    text-shadow : rgba(255,255,255,0.5) 0 5px 6px, rgba(255,255,255,0.2) 1px 3px 3px;
    -webkit-background-clip : text;
  }
</style>
